<?
	$page["title"] = "Configuration of triggers";
	$page["file"] = "triggers.html";

	include "include/config.inc";
	show_header($page["title"],0,0);
?>

<?
	if(isset($register))
	{
		if($register=="changestatus")
		{
			update_trigger_status($triggerid,$status);
		}
		if($register=="update")
		{
			if(validate_expression($expression)==0)
			{
				$now=mktime();
				if(isset($disabled))	{ $istrue=2; }
				else			{ $istrue=3; }
	
				update_trigger($triggerid,$expression,$description,$priority,$istrue,$comments);
			}
		}
		if($register=="add")
		{
			if(validate_expression($expression)==0)
			{
				if(isset($disabled))	{ $istrue=2; }
				else			{ $istrue=0; }
				
				add_trigger($expression,$description,$priority,$istrue,$comments);
			}
		}
		if($register=="delete")
		{
			delete_trigger($triggerid);
			unset($triggerid);
		}
	}
?>

<?
	show_table_header_begin();
	echo "CONFIGURATION OF TRIGGERS";
	show_table_v_delimiter();
?>

<?
	$result=DBselect("select hostid,host from hosts order by host");
	if(isset($hostid))
	{
		echo "<A HREF=\"triggers.html\">all</A>  ";
	}
	else
	{
		echo "<b>[<A HREF=\"triggers.html\">all</A>]</b>  ";
	}
	for($i=0;$i<DBnum_rows($result);$i++)
	{
		$hid=DBget_field($result,$i,0);
		$host=DBget_field($result,$i,1);
		if(isset($hostid) && ($hid == $hostid))
		{
			echo "<b>[<A HREF=\"triggers.html?hostid=$hid\">$host</A>]</b>  ";
		}
		else
		{
			echo "<A HREF=\"triggers.html?hostid=$hid\">$host</A>  ";
		}
	}

	show_table_header_end();
	echo "<br>";
?>

<?

	if(isset($hostid))
	{
		$cond=" and h.hostid=$hostid ";
	}
	else
	{
		$cond="";
	}

	$result=DBselect("select h.hostid,h.host,t.triggerid,t.expression,t.description,t.istrue from triggers t,hosts h,items i,functions f where f.itemid=i.itemid and h.hostid=i.hostid and t.triggerid=f.triggerid $cond order by h.host,t.description");
	$lasthost="";
	$col=0;
	for($i=0;$i<DBnum_rows($result);$i++)
	{
		$hostid_=DBget_field($result,$i,0);
		$host=DBget_field($result,$i,1);
		if($lasthost!=$host)
		{
			if($lasthost!="")
			{
				echo "</TABLE><BR>";
			}
			show_table_header("<A HREF='triggers.html?hostid=$hostid_'>$host</A>");
			echo "<TABLE BORDER=0 COLS=3 WIDTH=\"100%\" BGCOLOR=\"#CCCCCC\" cellspacing=1 cellpadding=3>";
			echo "<TR>";
			echo "<TD><B>Description</B></TD>";
			echo "<TD><B>Expression</B></TD>";
			echo "<TD WIDTH=\"5%\"><B>Status</B></TD>";
			echo "<TD WIDTH=\"15%\" NOSAVE><B>Actions</B></TD>";
			echo "</TR>\n";
		}
		$lasthost=$host;

	        if($col==1)
	        {
	                echo "<TR BGCOLOR=#DDDDDD>";
	                $col=0;
	        } else
	        {
	                echo "<TR BGCOLOR=#EEEEEE>";
	                $col=1;
	        }
		$description=DBget_field($result,$i,4);
		$triggerid_=DBget_field($result,$i,2);
		$expression=DBget_field($result,$i,3);
		$istrue=DBget_field($result,$i,5);
		echo "<TD>$description</TD>";
		$description=rawurlencode($description);

		echo "<TD>".explode_exp($expression,1)."</TD>";
		echo "<TD>";
		if($istrue == 2)
		{
			echo "<a href=\"triggers.html?register=changestatus&triggerid=$triggerid_&status=0&hostid=$hostid_\">Disabled";
		}
		else
		{
			echo "<a href=\"triggers.html?register=changestatus&triggerid=$triggerid_&status=2&hostid=$hostid_\">Enabled";
		}
		$expression=rawurlencode($expression);
		echo "</TD>";
		echo "<TD>";
		if(isset($hostid))
		{
			echo "<A HREF=\"triggers.html?triggerid=$triggerid_&hostid=$hostid#form\">Change</A> ";
		}
		else
		{
			echo "<A HREF=\"triggers.html?triggerid=$triggerid_#form\">Change</A> ";
		}
		echo "-<A HREF=\"actions.html?triggerid=$triggerid_&description=$description\">ShowActions</A>";
		echo "</TD>";
		echo "</TR>\n";
	}
	echo "</table>\n";
?>

<?
	$result=DBselect("select count(*) from hosts");
	if(DBget_field($result,0,0)>0)
	{
		echo "<a name=\"form\"></a>";
		@insert_trigger_form($hostid,$triggerid);
	} 
?>

<?
	show_footer();
?>
